local sk__jilis = fk.CreateSkill {

  name = "sk__jilis",

  tags = {  },

}



sk__jilis:addEffect(fk.CardUseFinished, {
  name = "sk__jilis",
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__jilis.name) and data.card.type ~= Card.TypeTrick and
      table.find(player.room:getOtherPlayers(player), function(p)
        return player:inMyAttackRange(p) and not p:isNude()
      end)
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, sk__jilis.name, nil, "#sk__jilis-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    local targets = table.filter(room:getOtherPlayers(player), function(p)
      return player:inMyAttackRange(p)
    end)
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    for _, p in ipairs(targets) do
      if not p.dead and not p:isNude() then
        n = n + 1
        local card = table.random(p:getCardIds("he"))
        room:throwCard(card, sk__jilis.name, p, p)
      end
    end
    if not player.dead and n > 0 and
      #room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
        local use = e.data
        return use and use.from == player
      end, Player.HistoryTurn) <= player:getAttackRange() then
      player:drawCards(n, sk__jilis.name)
    end
  end,
})
sk__jilis:addEffect(fk.CardRespondFinished, {
  name = "sk__jilis",
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__jilis.name) and data.card.type ~= Card.TypeTrick and
      table.find(player.room:getOtherPlayers(player), function(p)
        return player:inMyAttackRange(p) and not p:isNude()
      end)
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, sk__jilis.name, nil, "#sk__jilis-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    local targets = table.filter(room:getOtherPlayers(player), function(p)
      return player:inMyAttackRange(p)
    end)
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    for _, p in ipairs(targets) do
      if not p.dead and not p:isNude() then
        n = n + 1
        local card = table.random(p:getCardIds("he"))
        room:throwCard(card, sk__jilis.name, p, p)
      end
    end
    if not player.dead and n > 0 and
      #room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
        local use = e.data
        return use and use.from == player
      end, Player.HistoryTurn) <= player:getAttackRange() then
      player:drawCards(n, sk__jilis.name)
    end
  end,
})

return sk__jilis